package com.example.customizeview.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.customizeview.R;

public class RectView extends View {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Paint paint_text = new Paint(Paint.ANTI_ALIAS_FLAG);
    int mColor = Color.RED;
    String aa = "";

    public RectView(Context context) {
        super(context);
        initDraw();
    }

    public RectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);


        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RectView);

        mColor = typedArray.getColor(R.styleable.RectView_rect_color,Color.RED);

        aa = typedArray.getString(R.styleable.RectView_rect_text);
        Log.i("string",aa);
         // After getting the resource,instantly get back
         typedArray.recycle();

        initDraw();

    }

    public RectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initDraw();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);

        Log.i("widthSpecMode",widthSpecMode+"");
        Log.i("heightSpecMode",heightSpecMode+"");

        int widthSize  = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        Log.i("widthSpecSize",widthSize+"");
        Log.i("heightSpecSize",heightSize+"");


        // 这个模式一般就是区分 wrap_content or match_parent
        //AT_MOST == wrap_content   EXACTLY == match_parent
        if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,200);
        }else if(widthSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,widthSize);
        }else if(heightSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(heightSize,200);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width =  getWidth();
        int height = getHeight();

        int paddingleft = getPaddingLeft();
        int paddingright = getPaddingRight();
        int paddingtop = getPaddingTop();
        int paddingbottom = getPaddingBottom();


        int actualWidth = width - paddingright - paddingleft;
        int actualHeight = height - paddingtop - paddingbottom;

        Log.i("width",getWidth()+"");
        Log.i("height",getHeight()+"");

        // 120401002641907

        canvas.drawRect(0+paddingleft,0+paddingtop,actualWidth+paddingright,actualHeight+paddingtop,paint);
        canvas.drawText(aa,0+paddingleft,0+paddingtop,paint_text);
//        canvas.drawRect(0,0,width,height,paint);
    }

    private void initDraw(){
        paint.setColor(mColor);
        paint.setStrokeWidth(3.f);
        paint_text.setTextSize(33);
        paint_text.setColor(Color.BLUE);
    }


}
